{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Exercise -Version Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- This is the exercise based on the forked repository on GitHub\n",
    "-  Help to improve it if you want, just add the solution under the question\n",
    "- Each notebook only contains 10 questions which is convenient for everyone to locate each question to modify, you can add your own name under the author's list below, let's contribute together !\n",
    "- To visit my personal site: [Click Here](http://yaozeliang.com/resume)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 91\n",
    ">***Please write a program which accepts a string from console and print it in reverse order.***\n",
    "\n",
    ">**Example:\n",
    "If the following string is given as input to the program:***\n",
    "```\n",
    "rise to vote sir\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "ris etov ot esir\n",
    "```\n",
    "### Hints \n",
    "> ***Use list[::-1] to iterate a list in a reverse order.***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "s=\"rise to vote sir\"\n",
    "s[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s=input()\n",
    "s = s[::-1]\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 92\n",
    "\n",
    ">***Please write a program which accepts a string from console and print the characters that have even indexes.***\n",
    "\n",
    ">***Example:\n",
    "If the following string is given as input to the program:***\n",
    "```\n",
    "H1e2l3l4o5w6o7r8l9d\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "Helloworld\n",
    "```\n",
    "### Hints \n",
    ">***Use list[::2] to iterate a list by step 2.***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 93\n",
    "\n",
    ">***Please write a program which prints all permutations of [1,2,3]***\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    "> ***Use itertools.permutations() to get permutations of list.***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import itertools\n",
    "list(itertools.permutations([1,2,3]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 94\n",
    "\n",
    ">***Write a program to solve a classic ancient Chinese puzzle: \n",
    "We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def solve(numheads,numlegs):\n",
    "    ns='No solutions!'\n",
    "    for i in range(numheads+1):\n",
    "        j=numheads-i\n",
    "        if 2*i+4*j==numlegs:\n",
    "            return i,j\n",
    "    return ns,ns\n",
    "\n",
    "numheads=35\n",
    "numlegs=94\n",
    "solutions=solve(numheads,numlegs)\n",
    "solutions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 95\n",
    ">***Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given  scores. Store them in a list and find the score of the runner-up.***\n",
    "\n",
    ">***If the following string is given as input to the program:***\n",
    ">```\n",
    ">5\n",
    ">2 3 6 6 5\n",
    ">```\n",
    ">***Then, the output of the program should be:***\n",
    ">```\n",
    ">5\n",
    ">```\n",
    "### Hints \n",
    "> ***Make the scores unique and then find 2nd best number***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_20584/2113532419.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0marr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0marr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0msorted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "n = int(input())\n",
    "arr = map(int, input().split())\n",
    "arr = list(set(arr))\n",
    "sorted(arr)[-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 96\n",
    ">***You are given a string S and width W.\n",
    "Your task is to wrap the string into a paragraph of width.***\n",
    "\n",
    ">***If the following string is given as input to the program:***\n",
    ">```\n",
    ">ABCDEFGHIJKLIMNOQRSTUVWXYZ\n",
    ">4\n",
    ">```\n",
    ">***Then, the output of the program should be:***\n",
    ">```\n",
    ">ABCD\n",
    ">EFGH\n",
    ">IJKL\n",
    ">IMNO\n",
    ">QRST\n",
    ">UVWX\n",
    ">YZ\n",
    ">```\n",
    "\n",
    "### Hints\n",
    "> ***Use wrap function of textwrap module***|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import textwrap\n",
    "\n",
    "def wrap(string, max_width):\n",
    "    string = textwrap.wrap(string,max_width)\n",
    "    string = \"\\n\".join(string)\n",
    "    return string\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    string, max_width = input(), int(input())\n",
    "    result = wrap(string, max_width)\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools as it\n",
    "\n",
    "def grouper(lst, n, fillvalue=None):\n",
    "    iters = [iter(lst)] * n\n",
    "    return it.zip_longest(*iters, fillvalue=fillvalue)  #  默认就是None\n",
    "string, max_width = input(), int(input())\n",
    "result = grouper(string, max_width)\n",
    "print(list(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 97\n",
    "\n",
    "\n",
    ">***You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.)***\n",
    "\n",
    ">***Different sizes of alphabet rangoli are shown below:***\n",
    ">```\n",
    ">#size 3\n",
    ">\n",
    ">----c----\n",
    ">--c-b-c--\n",
    ">c-b-a-b-c\n",
    ">--c-b-c--\n",
    ">----c----\n",
    ">\n",
    ">#size 5\n",
    ">\n",
    ">--------e--------\n",
    ">------e-d-e------\n",
    ">----e-d-c-d-e----\n",
    ">--e-d-c-b-c-d-e--\n",
    ">e-d-c-b-a-b-c-d-e\n",
    ">--e-d-c-b-c-d-e--\n",
    ">----e-d-c-d-e----\n",
    ">------e-d-e------\n",
    ">--------e--------\n",
    ">```\n",
    "### Hints \n",
    ">***First print the half of the Rangoli in the given way and save each line in a list. Then print the list in reverse order to get the rest.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import string\n",
    "def print_rangoli(size):\n",
    "    n = size\n",
    "    alph = string.ascii_lowercase\n",
    "    width = 4 * n - 3\n",
    "\n",
    "    ans = []\n",
    "    for i in range(n):\n",
    "        left = '-'.join(alph[n - i - 1:n])\n",
    "        mid = left[-1:0:-1] + left\n",
    "        final = mid.center(width, '-')\n",
    "        ans.append(final)\n",
    "\n",
    "    if len(ans) > 1:\n",
    "        for i in ans[n - 2::-1]:\n",
    "            ans.append(i)\n",
    "    ans = '\\n'.join(ans)\n",
    "    print(ans)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    n = int(input())\n",
    "    print_rangoli(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 98\n",
    "\n",
    ">***You are given a date. Your task is to find what the day is on that date.***\n",
    "\n",
    "**Input**\n",
    ">***A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.***\n",
    ">```\n",
    ">08 05 2015\n",
    ">```\n",
    "\n",
    "\n",
    "**Output**\n",
    ">***Output the correct day in capital letters.***\n",
    ">```\n",
    ">WEDNESDAY\n",
    ">```\n",
    "\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    "> ***Use weekday function of calender module***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import calendar\n",
    "\n",
    "month, day, year = map(int, input().split())\n",
    "\n",
    "dayId = calendar.weekday(year, month, day)\n",
    "print(calendar.day_name[dayId].upper())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 99\n",
    ">***Given 2 sets of integers, M and N, print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either M or N but do not exist in both.***\n",
    "\n",
    "**Input**\n",
    ">***The first line of input contains an integer, M.The second line contains M space-separated integers.The third line contains an integer, N.The fourth line contains N space-separated integers.***\n",
    ">```\n",
    ">4\n",
    ">2 4 5 9\n",
    ">4\n",
    ">2 4 11 12\n",
    ">```\n",
    "\n",
    "**Output**\n",
    ">***Output the symmetric difference integers in ascending order, one per line.***\n",
    ">```\n",
    ">5\n",
    ">9\n",
    ">11\n",
    ">12\n",
    ">```\n",
    "\n",
    "\n",
    "----------------------\n",
    "### Hints\n",
    "> ***Use \\'^\\' to make symmetric difference operation.***\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == '__main__':\n",
    "    n = int(input())\n",
    "    set1 = set(map(int,input().split()))\n",
    "\n",
    "    m = int(input())\n",
    "    set2 = set(map(int, input().split()))\n",
    "\n",
    "    ans = list(set1 ^ set2)\n",
    "    print(sorted(ans))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 100\n",
    "\n",
    ">***You are given  words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.***\n",
    "\n",
    ">***If the following string is given as input to the program:***\n",
    ">```\n",
    ">4\n",
    ">bcdef\n",
    ">abcdefg\n",
    ">bcde\n",
    ">bcdef\n",
    ">```\n",
    ">***Then, the output of the program should be:***\n",
    ">```\n",
    ">3\n",
    ">2 1 1\n",
    ">```\n",
    "\n",
    "### Hints \n",
    "> ***Make a list to get the input order and a dictionary to count the word frequency***\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "n = int(input())\n",
    "\n",
    "word_list = []\n",
    "word_dict = {}\n",
    "\n",
    "for i in range(n):\n",
    "    word = input()\n",
    "    if word not in word_dict:\n",
    "        word_list.append(word)\n",
    "    word_dict[word] = word_dict.get(word, 0) + 1\n",
    "\n",
    "print(len(word_list))\n",
    "for word in word_list:\n",
    "    print(word_dict[word], end=' ')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
